package com.xxx.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Result;
import com.xxx.entity.MPage;
import com.xxx.entity.Produce;
import com.xxx.entity.User;
import com.xxx.entity.query.ProduceQuery;
import com.xxx.service.IProduceService;
import com.xxx.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xxx
 * @since 2024-11-24
 */
@RestController
@RequestMapping("/produce")
@Tag(name = "农产品管理")
public class ProduceController {

    @Autowired
    private IProduceService produceService;

    @Autowired
    private IUserService userService;


    @PostMapping("/add")
    @Operation(summary = "添加农产品")
    public Result add(@RequestBody Produce produce){
        Boolean flag = produceService.save(produce);
        return flag ? Result.success("添加农产品成功") : Result.error("添加农产品失败");
    }

    @GetMapping("/delete/{produceId}")
    @Operation(summary = "删除农产品")
    public Result delete(@PathVariable Integer produceId){
        Boolean flag = produceService.removeById(produceId);
        return flag ? Result.success("删除农产品成功") : Result.error("不存在该农产品Id");
    }

    @PutMapping("/update")
    @Operation(summary = "修改农产品")
    public Result update(@RequestBody Produce produce){
        Boolean flag = produceService.updateById(produce);
        return flag ? Result.success("修改农产品成功") : Result.error("修改农产品失败");
    }

    @GetMapping("/selectByPage/{current}/{size}")
    @Operation(summary = "分页查询农产品")
    public Result selectByPage(@PathVariable Integer current, @PathVariable Integer size){
//        IPage<Produce> page = produceService.page(new Page<>(current,size));
        MPage page = produceService.selectByPage(current, size);
        return Result.success(page);

    }

    @PostMapping("/selectByCondition")
    @Operation(summary = "根据名字、类型、卖家Id、农产品Id、卖家电话、卖家名称条件查询农产品")
    public Result selectByCondition(@RequestBody ProduceQuery produceQuery){
        MPage page = produceService.selectByCondition(produceQuery);
        return Result.success(page);
    }


}

